package net.exoweb.animate.actions
{
    import flash.display.*;

    import net.exoweb.animate.actions.Action;
    import net.exoweb.animate.common.Maths;
    import net.exoweb.animate.objects.Vector;

    public class UniformTranslation extends Action
    {
        private var _toPos:Vector;

        public function UniformTranslation(toPos:Vector, speed:Number)
        {
            super(speed);
            _toPos = new Vector(toPos.x, toPos.y);
        }

        public function get toPos():Vector
        {
            return _toPos;
        }

        public function set toPos(v:Vector):void
        {
            _toPos.equal(v);
        }

        override public function update(obj:DisplayObject, time:Number):void
        {
            var x:Number = _toPos.x - obj.x;
            var y:Number = _toPos.y - obj.y;
            var l:Number = Math.sqrt(x*x + y*y);
            if(l < Maths.EXP)
            {
                obj.x = _toPos.x;
                obj.y = _toPos.y;
                complete();
                return;
            }
            var a:Number = 1.0 / l;
            var dx:Number = time * _speed * a * x;
            var dy:Number = time * _speed * a * y;
            if((dx*dx + dy*dy) > (x*x + y*y))
            {
                obj.x = _toPos.x;
                obj.y = _toPos.y;
                complete();
            }
            else
            {
                obj.x += dx;
                obj.y += dy;
            }
        }
    }
}
